﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FileCopy
{
    public class f_File
    {
        private string srcDirectory;
        private string desDirectory;
        public string FileName { get; set; }
        /// <summary>
        /// 源文件完整路径
        /// </summary>
        public string SrcFileFullName
        {
            get
            {
                return Path.Combine(srcDirectory, FileName);
            }
        }

        /// <summary>
        /// 目标文件路径
        /// </summary>
        public string DesFileFullName
        {
            get
            {
                return Path.Combine(desDirectory, FileName);
            }
        }

        public string FileNameWithoutExtension
        {
            get
            {
                return Path.GetFileNameWithoutExtension(FileName);
            }
        }

        private string extension;
        public string Extension
        {
            get
            {
                if (string.IsNullOrEmpty(extension))
                    return Path.GetExtension(FileName);
                return extension;
            }
            set
            {
                extension = value;
            }
        }

        private DateTime createTime;
        /// <summary>
        /// 文件创建时间
        /// </summary>
        public DateTime CreateTime { get { return createTime; } }

        public f_File(string srcFileFulleName, string desPath, DateTime createTime)
        {
            this.FileName = Path.GetFileName(srcFileFulleName);
            this.srcDirectory = Path.GetDirectoryName(srcFileFulleName);
            this.desDirectory = desPath;
            this.createTime = createTime;
        }

        public void CopyTo()
        {
            try
            {
                string cmd = string.Format("copy /y {0} {1}", this.SrcFileFullName, this.DesFileFullName);
                using (System.Diagnostics.Process process = new System.Diagnostics.Process())
                {
                    process.StartInfo.FileName = "cmd.exe";
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();
                    process.StandardInput.WriteLine(cmd);
                    process.StandardInput.WriteLine("exit");
                    Console.WriteLine(process.StandardOutput.ReadToEnd());
                    process.Close();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.TraceWrite(ex.Message, string.Format("{0}->{1}", this.SrcFileFullName, this.DesFileFullName));
            }
            this.IsCompleted = true;
        }

        public bool IsCompleted { get; set; }

        public override string ToString()
        {
            return FileName;
        }
    }
}
